<!-- 图片列表显示 -->
<template>
  <div
    class="pdtable-img"
    v-if="imageList.length"
  >
    <div
      class="item"
      v-for="(item,index) in imageList"
      :key="index"
    >
      <div class="img">
        <img
          :src="item.src"
          :alt="item.alt"
          :title="item.title"
          @click.stop="clickImage(item.id)"
        />
      </div>
      <div class="opt">
        <!-- <el-button type="text" @click.stop="editorAttr(index)"><i class="el-icon-edit"></i></el-button> -->
        <el-button type="text" @click.stop="deleteImage(index)"><i class="el-icon-delete"></i></el-button>
      </div>
    </div>
    <WDialogSysImageAttr ref="wDialogImageAttr" />
  </div>
</template>

<script>
import WDialogSysImageAttr from '@/components/main/user/system/common/WDialogSysImageAttr.vue'
export default {
  props: {
    imageList: {
      type: Array
    },
    resetImageList: {
      type: Function
    },
    imageRepertoryConfig: {
      type: Object
    }
  },
  data () {
    return {
    }
  },
  methods: {
    clickImage (id) {
      let item = getItem(id, this.imageList)
      if (!item) {
        return
      }
      window.open(item.src)
    },
    deleteImage (index) {
      let list = []
      for (let i in this.imageList) {
        if (parseInt(i) === parseInt(index)) {
          continue
        }
        list.push(this.imageList[i])
      }
      this.resetImageList(JSON.parse(JSON.stringify(list)))
    },
    // 编辑图片属性
    editorAttr (index) {
      let item = this.imageList[index]
      if (!item) {
        return
      }

      this.$refs['wDialogImageAttr'].open({ alt: item.alt, title: item.title }, (image) => {
        this.imageList[index].alt = image.alt
        this.imageList[index].title = image.title
        this.imageRepertoryConfig.alt = image.alt
        this.imageRepertoryConfig.title = image.title
      })
    }
  },
  components: {
    WDialogSysImageAttr
  }
}

function getItem (id, list) {
  return list.find((item) => {
    if (id === item.id) {
      return true
    }
    return false
  })
}

</script>
<style scoped lang="scss">
.pdtable-img {
  display: flex;
  flex-flow: wrap;
  margin: 12px 0;
  .item {
    width: 180px;
    margin-right: 8px;
    position: relative;
    .img {
      line-height: 180px;
      text-align: center;
      border: 1px solid #e5e5e5;
      height: 180px;
      img {
        max-width: 170px;
        max-height: 170px;
        vertical-align: middle;
        cursor: pointer;
      }
    }
    .opt {
      position: absolute;
      top: 0;
      right: 0;
      background: #409EFF;
      line-height: 26px;
      // padding: 0 5px;
      // display: none;
      .el-button {
        color: #fff;
        padding:3px 5px;
        // margin: 0;
      }
    }
    // &:hover .opt{
    //   display: block;
    // }
  }
}
</style>
